#!/bin/bash

function error
{
  exit $1
}
NEWLANG=$1
if [ "$NEWLANG" = "ru_RU" ] ; then
NEWLANG=ru_RU.koi8r
fi

if [ "$1" = "" ] ; then
  echo "ERROR: Null Locale value"
  error 5
fi

sed "s/LANG=[\"]*[a-zA-Z_0-9\.][a-z_A-Z\.0-9]*[\"]*/LANG=\"${NEWLANG}\"/" /etc/sysconfig/i18n > /var/hsc/tmp/i18n

if [ $? != 0 ] ; then
  echo "ERROR: Could not create locale in /var/hsc/tmp"
  error $?
fi

cp /var/hsc/tmp/i18n /etc/sysconfig/i18n  2>/dev/null

if [ $? != 0 ] ; then
  echo "ERROR: Could not copy locale file to /etc/sysconfig"
  error $?
fi

rm /var/hsc/tmp/i18n 2>/dev/null

if [ $? != 0 ] ; then
  echo "ERROR: Could not remove /var/hsc/tmp/i18n"
fi

if [ "$NEWLANG" = "ru_RU.koi8r" ] ; then
  grep InputDevice /etc/X11/XF86Config-4 | grep KeyboardRussian
  if [ $? != 0 ] ; then
    /opt/hsc/bin/changeRussianKeyboard set
  fi
else
  grep InputDevice /etc/X11/XF86Config-4 | grep KeyboardRussian
  if [ $? == 0 ] ; then
    /opt/hsc/bin/changeRussianKeyboard unset
  fi
fi

/usr/websm/bin/configPccHtmls -lang $NEWLANG

if [ $? != 0 ] ; then
  echo "ERROR: Could not run /usr/websm/bin/configPccHtmls"
  error 2222
fi

exit 0
